home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 22 / Amiga Format AFCD22 (Jan 1998, Issue 106).iso / -seriously_amiga- / shareware / programming / other / pmdev / demos / demo.c < prev    next >
C/C++ Source or Header  |  1997-11-17  |  5KB  |  163 lines

  1. //
  2. // $VER: Demo.c 2.0 (16.10.97)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ©1996-1997 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // Run and click the mouse in the window and over the dragbar!
  10. // (two different menus)
  11. //
  12.  
  13. #include <intuition/intuition.h>
  14.  
  15. #include <clib/intuition_protos.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/alib_protos.h>
  18.  
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. #include <libraries/pm.h>
  24. #include <proto/pm.h>
  25.  
  26. struct IntuitionBase    *IntuitionBase;
  27. struct GfxBase        *GfxBase;
  28. struct PopupMenuBase    *PopupMenuBase;
  29.  
  30. struct Window *w;    // This window is only needed to find out when and where the menu should appear.
  31.             // The font in this window's rastport will be used for the menu.
  32.  
  33. ULONG __saveds __asm MenuHandlerFunc(register __a0 struct Hook *hook,
  34.                      register __a2 APTR object,
  35.                      register __a1 APTR msg)
  36. {
  37.     struct PopupMenu *pm=(struct PopupMenu *)object;
  38.     char bfr[128];
  39.  
  40.     if(pm->UserData) {
  41.         sprintf(bfr, "RUN <>NIL: %s", pm->UserData);
  42.         system(bfr);
  43.     }
  44.  
  45.     if(pm->ID==5) *((BOOL *)hook->h_Data)=FALSE;    // ID 5 means quit
  46. }
  47.  
  48. void main()
  49. {
  50.     BOOL r=TRUE;
  51.     struct IntuiMessage *im,imsg;
  52.     struct PopupMenu *p1,*p2;
  53.     struct Hook MenuHandler;
  54.  
  55.     MenuHandler.h_Entry=(HOOKFUNC)MenuHandlerFunc;
  56.     MenuHandler.h_Data=&r;
  57.  
  58.     PopupMenuBase=(struct PopupMenuBase *)OpenLibrary(POPUPMENU_NAME,POPUPMENU_VERSION);            // Open the library
  59.     if(PopupMenuBase) {
  60.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;    // We let popupmenu.library open the libraries we need
  61.         GfxBase=(struct GfxBase *)PopupMenuBase->pmb_GfxBase;            // They remain valid until the library is closed!
  62.  
  63.         p1=PMMenu("Run Demos"),
  64.             PMItem("Disable"), PMSimpleSub,
  65.                 PMInfo("This demo shows how to read the"),    End,
  66.                 PMInfo("state of a checkmark, and"),        End,
  67.                 PMInfo("how to disable & enable an item"),    End,
  68.                 PMInfo("and its submenu."),            End,
  69.                 PMBar,    End,
  70.                 PMItem("Run the demo!"),    PM_UserData,    "Disable",    End,
  71.                 End,
  72.             End,
  73.             PMItem("PullDownMenu"), PMSimpleSub,
  74.                 PMInfo("The PullDownMenu demo shows how"),    End,
  75.                 PMInfo("you can use the popupmenu.library"),    End,
  76.                 PMInfo("for pulldown menus!"),            End,
  77.                 PMBar,    End,
  78.                 PMItem("Run the demo!"),    PM_UserData,    "PullDownMenu",    End,
  79.                 End,
  80.             End,
  81.             PMItem("MenuVerify"), PMSimpleSub,
  82.                 PMInfo("This example uses the"),        End,
  83.                 PMInfo("IDCMP_MENUVERIFY flag to"),        End,
  84.                 PMInfo("combine standard intuition"),        End,
  85.                 PMInfo("menus with popupmenus int"),        End,
  86.                 PMInfo("the same window!"),            End,
  87.                 PMBar,    End,
  88.                 PMItem("Run the demo!"),    PM_UserData,    "MenuVerify",    End,
  89.                 End,
  90.             End,
  91.             PMItem("StartMenu"), PMSimpleSub,
  92.                 PMInfo("This demo shows how to put"),        End,
  93.                 PMInfo("images in you menus."),            End,
  94.                 PMInfo("You can find the menu at the"),        End,
  95.                 PMInfo("bottom of the screen."),        End,
  96.                 PMBar,    End,
  97.                 PMItem("Run the demo!"),    PM_UserData,    "StartMenu",    End,
  98.                 End,
  99.             End,
  100.         End;
  101.  
  102.         p2=PMMenu("PopupMenu Demo"),
  103.             PMItem("About..."),                End,
  104.             PMTitleBar,                    End,
  105.             PMItem("Quit"),        PM_ID,        5,    End,
  106.         End;
  107.  
  108.         if(p1 && p2) {
  109.             w=OpenWindowTags(NULL,    WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,    // Open a little window
  110.                     WA_RMBTrap,    TRUE,
  111.                     WA_DragBar,    TRUE,
  112.                     WA_Left,    150,
  113.                     WA_Top,        100,
  114.                     WA_Width,    150,
  115.                     WA_Height,    100,
  116.                     WA_SizeGadget,    TRUE,
  117.                     WA_DepthGadget,    TRUE,
  118.                     WA_MinWidth,    150,
  119.                     WA_MaxWidth,    1280,
  120.                     WA_MinHeight,    100,
  121.                     WA_MaxHeight,    1024,
  122.                     WA_Title,    "Popup Menu Demo",
  123.                     WA_CloseGadget,    TRUE,
  124.                     TAG_DONE);
  125.             if(w) {
  126.  
  127.                 while(r) {
  128.                     WaitPort(w->UserPort);                        // Wait for a message
  129.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {    // Get the message
  130.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));        // Copy the contents of it
  131.                         ReplyMsg((struct Message *)im);                // Reply the message
  132.  
  133.                         switch(imsg.Class) {
  134.                             case IDCMP_CLOSEWINDOW: r=FALSE; break;
  135.                             case IDCMP_MOUSEBUTTONS:            // The user has hit a mousebutton - time to open the menu!
  136.                                 if(imsg.Code==MENUDOWN || imsg.Code==MENUUP) {
  137.                                     if(w->MouseY>w->BorderTop) {
  138.                                         PM_OpenPopupMenu(w,
  139.                                             PM_Menu,        p1,
  140.                                             PM_Code,        imsg.Code,
  141.                                             PM_MenuHandler,        &MenuHandler,
  142.                                             TAG_DONE);
  143.                                     } else {
  144.                                         PM_OpenPopupMenu(w,
  145.                                             PM_Menu,        p2,
  146.                                             PM_Code,        imsg.Code,
  147.                                             PM_MenuHandler,        &MenuHandler,
  148.                                             TAG_DONE);
  149.                                     }
  150.                                 }
  151.                             break;
  152.                         }
  153.                     }
  154.                 }
  155.                 CloseWindow(w);
  156.             } else printf("Window error!\n");
  157.         } else printf("Menu error!\n");
  158.         if(p1) PM_FreePopupMenu(p1);
  159.         if(p2) PM_FreePopupMenu(p2);
  160.         CloseLibrary((struct Library *)PopupMenuBase);
  161.     }
  162. }
  163.